package com.itheima.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author 王加鑫
 * @Date 2021/08/03 18:47
 * @Since JDK 1.8
 */
@Component
@Aspect
public class MyAdviceByXml {

    //切点
    @Pointcut("execution(* *..AopService.*(..))")
    public void pt(){

    }

    /**
     * @Before : 通知
     * @Before("pt()") : 引入
     * @param jp
     */
    @Before("pt()")
    public void before(JoinPoint jp){
        Object[] args = jp.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
        //获取标签
        Signature signature = jp.getSignature();
        String name = signature.getName();
        String declaringTypeName = signature.getDeclaringTypeName();
/*      Class type = signature.getDeclaringType();
        System.out.println("type = " + type);
        System.out.println("declaringTypeName = " + declaringTypeName);
        System.out.println("name = " + name);*/
        System.out.println(declaringTypeName + "." + name + " before run ...");
    }


    @After("pt()")
    public void after(){
        System.out.println(" after run ...");
    }

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp){
        System.out.println("around before ...");
        Object o = null;
        try {
            o = pjp.proceed();
        } catch (Throwable throwable) {
            System.out.println("throwable = " + throwable);;
        }
        System.out.println("around after ..." + o);
        return o;
    }

    @AfterReturning(value = "pt()", returning = "o")
    public void afterRunning(Object o){
        System.out.println("afterRunning run ..." + o);
    }

    @AfterThrowing(value = "pt()", throwing = "e")
    public void exception(Exception e){
        System.out.println("exception  run ..." + e);
    }

}
